package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.impl.UserDaoImpl;
import com.ysd.entity.User;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminMessageServlet extends BaseServlet {
	 /**
	    * 查询出留言板的所有数据
	    * @param localhost:8080/shangcheng1/adminMessage?reqName=getMessageByStatus
	    * @param response
	    * @throws ServletException
	    * @throws IOException
	    */
	public void getMessageByStatus(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		UserDaoImpl uDao = new UserDaoImpl();
		
		String status = request.getParameter("status");
		int page = Integer.parseInt(request.getParameter("page"));
		int size = Integer.parseInt(request.getParameter("size"));
		List<User> uList = uDao.getUserByStatus(status, page, size);
		
		Gson gson = new Gson();
		String res = gson.toJson(uList);
		out.print(res);
		
		out.flush();
		out.close();
		
	}

	 /**
	    * 查询出留言板的所有数据
	    * @param localhost:8080/shangcheng1/adminMessage?reqName=getMessageByStatusNum
	    * @param response
	    * @throws ServletException
	    * @throws IOException
	    */
	public void getMessageByStatusNum(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		UserDaoImpl uDao = new UserDaoImpl();
		
		String status = request.getParameter("status");
		List<User> uList = uDao.getUserByStatusNum(status);
		
		Gson gson = new Gson();
		String res = gson.toJson(uList.size());
		out.print(res);
		
		out.flush();
		out.close();
		
	}
	
	/**
	    * 查询出留言板的所有数据
	    * @param localhost:8080/shangcheng1/adminMessage?reqName=getMessageById
	    * @param response
	    * @throws ServletException
	    * @throws IOException
	    */
	public void getMessageById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		UserDaoImpl uDao = new UserDaoImpl();
		
		int Id = Integer.parseInt(request.getParameter("id"));
		User user = uDao.getUserById(Id);
		
		Gson gson = new Gson();
		String res = gson.toJson(user);
		out.print(res);
		
		out.flush();
		out.close();
		
	}
	
	/**
	    * 修改留言的状态为已读
	    * @param localhost:8080/shangcheng1/adminMessage?reqName=updateMessageById
	    * @param response
	    * @throws ServletException
	    * @throws IOException
	    */
	public void updateMessageById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		UserDaoImpl uDao = new UserDaoImpl();
		
		int id = Integer.parseInt(request.getParameter("id"));
		User user = uDao.getUserById(id);//根据id查询留言信息
		
		if (user.getStatus().equals("已读") || user.getStatus().equals("已回复")) {//如果留言状态查到是已读或者已回复则不用修改
			out.print(Result.toClient(true, "不需要修改", ""));
		} else {
			user = new User(id, "已读", null);
			int n = uDao.updateMessageById(user);
			if (n > 0) {
				out.print(Result.toClient(true, "修改成功", ""));
			} else {
				out.print(Result.toClient(false, "修改失败", ""));
			}
			
		}
		
		out.flush();
		out.close();
		
	}
	
	/**
	    * 修改留言的状态为已回复
	    * @param localhost:8080/shangcheng1/adminMessage?reqName=updateMessageReplyById
	    * @param response
	    * @throws ServletException
	    * @throws IOException
	    */
	public void updateMessageReplyById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		UserDaoImpl uDao = new UserDaoImpl();
		
		int id = Integer.parseInt(request.getParameter("id"));
		String reply = request.getParameter("reply");
		User user = new User(id, null,reply);
		int n = uDao.updateMessageReplyById(user);
		
		if (n > 0) {
			out.print(Result.toClient(true, "回复成功", ""));
		} else {
			out.print(Result.toClient(false, "回复失败", ""));
		}
		
		out.flush();
		out.close();
		
	}
	
	/**根据留言id删除
	 * localhost:8080/shangcheng1/adminMessage?reqName=delUserById
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delUserById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取前台传来的Id
		int id = Integer.parseInt(request.getParameter("id"));
		UserDaoImpl uDao = new UserDaoImpl();
		
		
		int n = uDao.delMessageById(id);
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		if (n > 0) {//删除成功
			out.print(Result.toClient(true, "删除成功", ""));
		} else {//删除失败
			out.print(Result.toClient(false, "删除失败", ""));
		}
		
		out.flush();
		out.close();
		
	}
	
	/**
	 * 根据多条产品id删除产品
	 * localhost:8080/shangcheng1/adminMessage?reqName=delUserByIds
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delUserByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		UserDaoImpl dDao = new UserDaoImpl();
		String ids = request.getParameter("ids");
		int n = dDao.delUserByIds(ids);
		
				
			if (n > 0) {
				out.print(Result.toClient(true, "修改成功", ""));
			} else {
				out.print(Result.toClient(false, "修改失败", ""));
			}
			
			out.flush();
			out.close();

	}
	
}